<?php
/*
 * 商品类
 *
 */
namespace Home\Logic;

class ShopitemimageLogic{
    
    public function __construct() {
        $this->si = D('Shop_item_image');

        import('Memcachectr');
        $this->m = \Think\Memcachectr::create();
    }
    
    public function setStoreId($store_id){
        $this->si->setTableNamePre($store_id);

    }
    
    public function getImageByItemId($id,$store_id){
        if($store_id<1){
            throw new \Exception('shop_id is null');
        }
        $this->setStoreId($store_id);
        $images = $this->m->get('item_images_'.$store_id."_".$id);
        if(!$images){
            $images = $this->si->where('item_id='.$id." and status=1")->select();
            $this->m->set('item_images_'.$store_id."_".$id,$images,true);
        }
        return $images;
    }
    
    public function getImageByItemNumber($number,$store_id){
        if($store_id<1){
            throw new \Exception('shop_id is null');
        }
        $this->setStoreId($store_id);
        $images = $this->m->get('item_images_number_'.$number);
        if(!$images){
            $images = $this->si->where("item_number='$number' and status=1")->select();
            $this->m->set('item_images_number_'.$number,$images,true);
        }
        return $images;
    }
    
    public function updateImage($item_id,$data,$store_id){
        if($store_id<1){
            throw new \Exception('shop_id is null');
        }
        $this->setStoreId($store_id);
        $where = 'item_id='.$item_id;
        $re = $this->si->where($where)->save($data);
        $this->m->delete('item_images_'.$item_id);
        return $re;
    }
    
    public function insertImage($data){
        if($data['store_id']<1){
            throw new \Exception('shop_id is null');
        }
        $this->setStoreId($data['store_id']);
        $this->m->delete('item_images_'.$data['store_id']."_".$data['item_id']);
        return $this->si->add($data);

    }

    public function thingUpdateImage($data,$getItem,$store_id){
        foreach($data['image'] as $k=>$v){
            if(strlen($v)<1){
                continue;
            }
            $item_id = $getItem['images'][$k]['item_id'];
            $status = array('status'=>0);
            $re[$k] = $this->updateImage($item_id,$status,$store_id);
        }
        $count = count($re);
        for($i=0;$i<$count;$i++){
            if($re[$i]===false){
                return false;
            }
        }
        return true;
    }

    public function thingInsertImage($data,$type){
        if($type==0){
            foreach($data['image'] as $k=>$v){
                if(strlen($v)<1){
                        continue;
                }
                $img['status'] =1;
                $img['create_time'] = time();
                $img['image'] = str_replace('./', '/', $v);
                $img['item_id'] = $data['item_id'];
                $img['item_number'] = $data['item_number'];
                $img['store_id'] = $data['store_id'];
                $re[$k] = $this->insertImage($img);
            }
            
        }elseif($type==1){
            foreach($data['image'] as $k=>$v ){
                    if(strlen($v)<1){
                        continue;
                    } 
                    $img['image'] = str_replace('./', '/', $v);
                    $img['item_number'] = $data['item_number'];
                    $img['item_id'] = $data['item_id'];
                    $img['status'] = $data['status'];
                    $img['create_time'] = $data['create_time'];
                    $img['store_id'] = $data['store_id'];
                    $re[$k] = $this->insertImage($img);
            } 
        }
        $count = count($re);
        for($i=0;$i<$count;$i++){
            if($re[$i]===false || $re[$i]==NULL){
                return false;
            }
        }
        return true;
    }

    
}